package com.jumi.microservice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.ResultEnum;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.IResponseRxceptionCode;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.ExpressDO;
import com.jumi.microservice.domain.ExpressDTO;
import com.jumi.microservice.mapper.ExpressMapper;
import com.jumi.microservice.service.ExpressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 商城物流表 服务实现类
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@Service
@Transactional
public class ExpressServiceImpl extends ServiceImpl<ExpressMapper, ExpressDO> implements ExpressService {

    /**
     * 新增物流公司
     * @param dto
     */
    @Override
    public void insert(ExpressDTO dto) {
        ExpressDO po = dto2Po(dto);
        ExceptionEnum.FAIL_EXPRESS_NULL.doThrowIf(po==null);
        ifExist(dto.getExpressName(), null);
        LocalDateTime now = LocalDateTime.now();
        po.setCreateTime(now);
        po.setUpdateTime(now);
        baseMapper.insert(po);
    }

    /**
     * 编辑物流公司
     * @param dto
     */
    @Override
    public void updateEx(ExpressDTO dto) {
        ExpressDO po = dto2Po(dto);
        ifExist(dto.getExpressName(), dto.getId());
        ExceptionEnum.FAIL_EXPRESS_NULL.doThrowIf(po==null);
        po.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(po);
    }

    /**
     * 删除物流公司
     * @param ids 主键id
     * @param username 操作用户
     */
    @Override
    public void delete(String ids, String username) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(ids==null);
        //判断否为启动状态
        String[] idss = ids.split(",");
        Arrays.stream(idss).forEach(id->{
            ExpressDO po = baseMapper.selectById(id);
            //启动状态抛出信息
            if(po!=null&& StatusEnum.STATUS_YES.getCode()==po.getStatus()){
                throw new BaseException(new IResponseRxceptionCode(ResultEnum.FAIL.getCode(), StrUtil.format(ExceptionEnum.FAIL_EXPRESS_STATUS.getMessage(),po.getId())));
            }
            po.setIsDel(StatusEnum.STATUS_YES.getCode());
            po.setUpdateTime(LocalDateTime.now());
            po.setModifier(username);
            baseMapper.updateById(po);
        });
    }

    /**
     * 物流公司详情
     * @param id 主键id
     * @return
     */
    @Override
    public ExpressDTO selectById(Long id) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        return po2Dto(baseMapper.selectById(id));
    }

    /**
     * 启用/禁用
     * @param id 抓紧id
     * @param status 状态，1启用，0.禁用
     * @param username  操作用户
     */
    @Override
    public void enableStatus(Long id, Integer status, String username) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        ExpressDO po = baseMapper.selectById(id);
        ExceptionEnum.FAIL_EXPRESS_NULL.doThrowIf(po==null);
        po.setStatus(status);
        po.setUpdateTime(LocalDateTime.now());
        po.setModifier(username);
        baseMapper.updateById(po);
    }

    /**
     * dto->po
     * @param dto
     * @return
     */
    private ExpressDO dto2Po(ExpressDTO dto){
        ExpressDO po = null;
        try {
            po = dto.clone(ExpressDO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return po;
    }

    /**
     * po->dto
     * @param po
     * @return
     */
    private ExpressDTO po2Dto(ExpressDO po){
        ExpressDTO dto = null;
        try {
            dto = po.clone(ExpressDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return dto;
    }

    private void ifExist(String name, Long id){
        int count = baseMapper.selectCount(
                Wrappers.<ExpressDO>lambdaQuery()
                        .eq(ExpressDO::getExpressName, name)
                        .eq(ExpressDO::getIsDel, StatusEnum.STATUS_NO.getCode())
                        .ne(id!=null, ExpressDO::getId, id));
        ExceptionEnum.FAIL_EXPRESS_EXIST.doThrowIf(count> NumberConstant.NUMBER_ZERO);
    }
}
